package com.bootdo.msg;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SendMsg {

    public static void main(String[] args) throws IOException {
        //读取相关数据，发送短息
        readDesNoFlie().forEach(SendMsg::send);
    }

    private static void send(String DesNo) {
        ArrayList<BasicNameValuePair> nvps = new ArrayList<>();
        BasicNameValuePair userCode = new BasicNameValuePair("userCode", Msginstance.user);
        nvps.add(userCode);
        nvps.add(new BasicNameValuePair("userPass", Msginstance.pwd));
        nvps.add(new BasicNameValuePair("Msg", Msginstance.code));
        nvps.add(new BasicNameValuePair("Channel", "0"));
        nvps.add(new BasicNameValuePair("DesNo", DesNo));
        //发送短信。。。。。。。。
        httpPost(Msginstance.url, nvps);
    }

    /**
     * 读取文件中的内容
     *
     * @return LIst
     * @throws IOException
     */
    public static List<String> readDesNoFlie() throws IOException {
        File file = new File("/Users/xiao/IdeaProjects/bootdo/bootdo/src/main/java/com/bootdo/msg/hh");
        String encoding = "UTF-8";
        List<String> lineTxtList = null;
        if (file.isFile() && file.exists()) { //判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String lineTxt = null;
            lineTxtList = new ArrayList<String>();
            while ((lineTxt = bufferedReader.readLine()) != null) {
                lineTxtList.add(lineTxt);
            }
            read.close();
        }
        return lineTxtList;
    }

    public static String httpPost(String url, ArrayList<BasicNameValuePair> params) {
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instreams = entity.getContent();
                result = convertStreamToString(instreams);
                System.out.println(result);
            }
        } catch (Exception e) {
        }
        return result;
    }


    public static String httpGet(String url, String params) {
        String result = "";
        try {
            HttpClient client = new DefaultHttpClient();
            if (params != "") {
                url = url + "?" + params;
            }
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = client.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instreams = entity.getContent();
                result = convertStreamToString(instreams);
                System.out.println(result);
            }
        } catch (Exception e) {
        }
        return result;
    }

    public static String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }


}
